import { _decorator, Component, instantiate, Label, Node, Prefab, RichText } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import { Soldier } from '../entity/Soldier';
import ResManager from '../common/ResManager';
import { SoldierItem } from './SoldierItem';
import { Utils } from '../common/Utils';
const { ccclass, property } = _decorator;

@ccclass('VillageRes')
export class VillageRes extends Component {
    @property(Node)
    public mask:Node;
    @property(Node)
    public close:Node;
    @property(Label)
    woodReserve:Label
    @property(Label)
    mudReserve:Label
    @property(Label)
    ironReserve:Label
    @property(RichText)
    foodReserve:RichText
    @property(RichText)
    public consume:RichText
    @property(Label)
    public wood:Label
    @property(Label)
    public food:Label
    @property(Label)
    public iron:Label
    @property(Label)
    public mud:Label
    @property(Label)
    public timeDown:Label
   
    @property(Node)
    public armyLayout:Node

    async start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)

        await UserInfo.instance.refreshVillageDetail()
        this.renderUI()
        this.renderSoldier()
    }

    update(deltaTime: number){
        let villageData = UserInfo.instance.villageData
        // 每秒粮食的增加量
        let addFood = (villageData.res.food_output - villageData.Consume) / 3600
        villageData.res.food_reserve += addFood * deltaTime
        if(villageData.res.food_reserve < 0){
            // UserInfo.instance.refreshVillageDetail()
            return
        }

        villageData.res.wood_reserve += villageData.res.wood_output / 3600 * deltaTime
        villageData.res.iron_reserve += villageData.res.iron_output / 3600 * deltaTime
        villageData.res.mud_reserve += villageData.res.mud_output / 3600 * deltaTime

        this.renderUI()
    }

    renderUI(){
        

        let villageData = UserInfo.instance.villageData
        if(villageData.res.wood_reserve > villageData.res.comm_max_reserve){
            villageData.res.wood_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.mud_reserve > villageData.res.comm_max_reserve){
            villageData.res.mud_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.iron_reserve > villageData.res.comm_max_reserve){
            villageData.res.iron_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.food_reserve > villageData.res.food_max_reserve){
            villageData.res.food_reserve = villageData.res.food_max_reserve
        }

        this.woodReserve.string = villageData.res.wood_reserve.toFixed() + ' / ' + villageData.res.comm_max_reserve
        this.mudReserve.string = villageData.res.mud_reserve.toFixed() + ' / ' + villageData.res.comm_max_reserve
        this.ironReserve.string = villageData.res.iron_reserve.toFixed() + ' / ' + villageData.res.comm_max_reserve
        
        
        if(UserInfo.instance.isFoodIncrease){
            this.foodReserve.string = `<b><color=DACAB3>${villageData.res.food_reserve.toFixed()} / ${villageData.res.food_max_reserve}</color></b>`
            this.consume.string =`<b><color=DACAB3>${villageData.res.parent.Consume} / ${villageData.res.food_output}</color></b>`
            this.timeDown.node.active = false
        } else {
            this.foodReserve.string = `<b><color=red>${villageData.res.food_reserve.toFixed()}</color><color=DACAB3> / ${villageData.res.food_max_reserve}</color></b>`
            this.consume.string =`<b><color=red>${villageData.res.parent.Consume}</color><color=DACAB3> / ${villageData.res.food_output}</color></b>`
            
            let offset = villageData.res.parent.Consume - villageData.res.food_output
            let sec = villageData.res.food_reserve / offset * 3600
            sec = Math.floor(sec)
            this.timeDown.string = Utils.secondToStr(sec) +  ' 后粮食耗尽'
        }
        
        this.wood.string = villageData.res.wood_output
        this.mud.string = villageData.res.mud_output
        this.iron.string = villageData.res.iron_output
        this.food.string = (villageData.res.food_output - villageData.res.parent.Consume) + ''

     
        
    }

    async renderSoldier(){
        let soldierList = Soldier.createBySoldiersString()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.SoldierItem)
        for(let so of soldierList){
            let node = instantiate(prefab)
            this.armyLayout.addChild(node)
            node.getComponent(SoldierItem).setData(so)
        }
    }

    
    onClose(){
        UIManager.instance.closeUI(PrefabEnum.VillageResources)
    }
}


